using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Search : System.Web.UI.Page
{
    int pageSize = 4;
    ControlHelper controlHelper;
    string connectionString =
    WebConfigurationManager.ConnectionStrings[
    "InterShop"].ConnectionString;
    string whereClause = "";
    
    protected void Page_Load(object sender, EventArgs e)
    {

        if(IsPostBack)
        {
            Literal1.Text = "";
            controlHelper = new
            ControlHelper(connectionString);
            controlHelper.CommandText =
            "select I.ItemID, ISBN, " +
            " ItemName, SalesPrice from " +
            " tblItems I, tblItemDetails D" +
            " where I.ItemID=D.ItemID ";
        }
        
    }
    protected void linkFirst_Click(
    object sender, EventArgs e)
    {
        whereClause = GetWhereClause();
        if (whereClause == "")
            this.MultiView1.ActiveViewIndex = 0;
        controlHelper.CommandText += whereClause;
        controlHelper.CommandType =
        CommandType.Text;
        controlHelper.InitProperties();
        controlHelper.FillDataTableToDataList(
        this.DataList1, 0, pageSize);
        lblPage.Text = "1";

        linkPrevious.Enabled = false;
        linkFirst.Enabled = false;
        linkNext.Enabled = true;
        linkLast.Enabled = true;
    }
    protected void linkPrevious_Click(
    object sender, EventArgs e)
    {
        linkNext.Enabled = true;
        linkLast.Enabled = true;
        int currentPage =
        Convert.ToInt32(lblPage.Text);
        if (currentPage > 1) currentPage--;
        whereClause = GetWhereClause();
        if (whereClause == "")
            this.MultiView1.ActiveViewIndex = 0;
        controlHelper.CommandText += whereClause;
        controlHelper.CommandType =
        CommandType.Text;
        controlHelper.InitProperties();
        controlHelper.FillDataTableToDataList(
        this.DataList1, (currentPage - 1) *
        pageSize, pageSize);
        lblPage.Text = currentPage.ToString();

        if (currentPage == 1)
        {
            linkPrevious.Enabled = false;
            linkFirst.Enabled = false;
        }
        else
        {
            linkFirst.Enabled = true;
            linkPrevious.Enabled = true;
        }
    }

    protected void linkNext_Click(
    object sender, EventArgs e)
    {
        linkFirst.Enabled = true;
        linkPrevious.Enabled = true;
        int currentPage =
        Convert.ToInt32(lblPage.Text);
        if (currentPage > 0) currentPage++;

        whereClause = GetWhereClause();
        if (whereClause == "")
            this.MultiView1.ActiveViewIndex = 0;
        controlHelper.CommandText += whereClause;
        controlHelper.CommandType =
        CommandType.Text;
        controlHelper.InitProperties();
        
        controlHelper.FillDataTableToDataList(
        this.DataList1, 
        (currentPage - 1) * pageSize, pageSize);
        lblPage.Text = currentPage.ToString();

        if (currentPage ==
        Convert.ToInt32(lblPages.Text))
        {
            linkLast.Enabled = false;
            linkNext.Enabled = false;
        }
        else
        {
            linkLast.Enabled = true;
            linkNext.Enabled = true;
        }
    }


    protected void linkLast_Click(
    object sender, EventArgs e)
    {
        linkNext.Enabled = false;
        linkLast.Enabled = false;
        linkFirst.Enabled = true;
        linkPrevious.Enabled = true;
        whereClause = GetWhereClause();
        if (whereClause == "")
            this.MultiView1.ActiveViewIndex = 0;
        controlHelper.CommandText += whereClause;
        controlHelper.CommandType =
        CommandType.Text;
        controlHelper.InitProperties();
        controlHelper.FillDataTableToDataList(
        this.DataList1, (Convert.ToInt32(lblPages.Text)
        - 1) * pageSize, pageSize);
        lblPage.Text = lblPages.Text;

    }

    protected void DataList1_ItemCommand(
    object source, DataListCommandEventArgs e)
    {
        DataListItem dataListItem = e.Item;
        Label labelID = 
        (Label)dataListItem.FindControl("lblID");
        HyperLink hyperLink = 
        (HyperLink)dataListItem.FindControl(
        "hyperLinkName");
        Label labelPrice = 
        (Label)dataListItem.FindControl(
        "lblPrice");
        Literal1.Text = "You just added \"" 
        + hyperLink.Text + "\" to Shopping cart";
        AddToShoppingCart(labelID.Text, 
        hyperLink.Text, labelPrice.Text);
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        this.MultiView1.ActiveViewIndex=1;
        ShowData();
    }
    string  GetWhereClause( )
    {
        string whereClause="";
        
        if (this.MultiView1.ActiveViewIndex == 1)
        {
             controlHelper.ParameterCollection 
             = new string[3] {"@Keyword" , 
             "@SalesPriceMin", "@SalesPriceMax"};
             controlHelper.ValueCollection 
             = new string[3] { "" , "", "" };
            if (txtKeyword.Text != "")
            {               
                whereClause +=  
                " and (ItemName like @Keyword "+
                " or ISBN = @Keyword "
               + " or Keywords like @Keyword)";
                controlHelper.ValueCollection[0] 
                = "%" + txtKeyword.Text + "%";
                 
            }


            if (txtPriceFrom.Text != "")
            {

                
                whereClause += 
                " and SalesPrice >=@SalesPriceMin";
                controlHelper.ValueCollection[1]
                 = txtPriceFrom.Text;
            }

            if (txtPriceTo.Text != "")
            {

                
                whereClause += 
                " and SalesPrice <=@SalesPriceMax";
                controlHelper.ValueCollection[2]
                 = txtPriceTo.Text;
            }
        }
        return whereClause;
    }
    void ShowData()
    {
        
        if (this.MultiView1.ActiveViewIndex == 1)
        {
            whereClause = GetWhereClause();
            if (whereClause == "")
                this.MultiView1.ActiveViewIndex = 0;
            controlHelper.CommandText += whereClause;
            controlHelper.CommandType =
            CommandType.Text;
            controlHelper.InitProperties();
            
            controlHelper.FillDataTableToDataList(
            this.DataList1, 0, pageSize);
                controlHelper.CommandText =
            "select count(*) from tblItems I, " +
            " tblItemDetails D  " +
            " where I.ItemID=D.ItemID " ;
            controlHelper.CommandText += whereClause;
            controlHelper.CommandType = CommandType.Text;
            controlHelper.InitProperties();
            int rows = controlHelper.GetTotalRecords();
            lblPages.Text = (rows / pageSize
            + ((rows % pageSize) > 0 ? 1 : 0)).ToString();

            if (rows <= pageSize || rows == 0)
            {
                linkPrevious.Visible = false;
                linkFirst.Visible = false;
                linkLast.Visible = false;
                linkNext.Visible = false;
            }
            else
            {
                linkPrevious.Enabled = false;
                linkFirst.Enabled = false;
            }
            if (rows == 0)
            {
                lblPage.Text = "0";
                Literal1.Text = "Item not found!";
            }
        } 
    }
    void AddToShoppingCart(string itemID, string itemName, string itemPrice)
    {
        DataTable dataTable;
        if(Session["ShoppingCart"] == null)
        {
            dataTable = new DataTable();
            dataTable.Columns.Add("ID");
            dataTable.Columns.Add("Name");
            dataTable.Columns.Add("Quantity");
            dataTable.Columns.Add("Price");
        }
        else
        {
            dataTable= 
            (DataTable)Session["ShoppingCart"];            
        }
        Common cls = new Common();
        int indexOfItem = 
        cls.IsExistItemInShoppingCart(itemID, dataTable);
        if (indexOfItem != -1)
        {
            dataTable.Rows[
            indexOfItem]["Quantity"] = 
            Convert.ToInt32(
            dataTable.Rows[
            indexOfItem]["Quantity"]) + 1;
        }
        else
        {
            DataRow dataRow = dataTable.NewRow();
            dataRow["ID"] = itemID;
            dataRow["Name"] = itemName;
            dataRow["Quantity"] = "1";
            dataRow["Price"] = itemPrice;
            dataTable.Rows.Add(dataRow);
        }
        Session["ShoppingCart"] = dataTable; 
    }
     
    protected void lkbBackToSearch_Click(
    object sender, EventArgs e)
    {
        this.MultiView1.ActiveViewIndex = 0;
    }
    
    protected void DataList1_ItemDataBound(
    object sender, DataListItemEventArgs e)
    { 
        DataListItem dataListItem = e.Item;
        HyperLink hyperLink = 
        (HyperLink)dataListItem.FindControl(
        "hyperLinkName");
        Label labelID = 
        (Label)dataListItem.FindControl("lblID");
        hyperLink.NavigateUrl = 
        "Details.aspx?ItemID=" + labelID.Text;
       
    }
}
